package com.appclub.kitsilano.other;

import java.util.Calendar;
import com.appclub.kitsilano.R;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.IBinder;

public class Service_ extends Service {

	private int Year;
	private int Day;
	private int Hour;
	private int Minute;
	private int D_M;
	private int D_W;
	private int Month;
	private int Mode;
	private SharedPreferences.Editor editor;
	private String PREFS_NAME;
	private SharedPreferences settings;
	private boolean ear = false;
	private BroadcastReceiver receiver;

	@Override
	public void onStart(Intent intent, int startId) {
		PREFS_NAME = "Kitsilano";
		settings = getSharedPreferences(PREFS_NAME, 0);
		editor = settings.edit();

		ear = settings.getBoolean("ear", false);

		boolean state_setting = settings.getBoolean("Setting", false);
		if (state_setting == false) {
			this.stopSelf();
		}
		receiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				int IPPDAYFROMHOUR = settings.getInt("IPPDAYFROMHOUR", 9);
				int IPPDAYFROMMIN = settings.getInt("IPPDAYFROMMIN", 34);
				int IPPDAYTOHOUR = settings.getInt("IPPDAYTOHOUR", 14);
				int IPPDAYTOMIN = settings.getInt("IPPDAYTOMIN", 28);

				int DAY1FROMHOUR = settings.getInt("DAY1FROMHOUR", 8);
				int DAY1FROMMIN = settings.getInt("DAY1FROMMIN", 54);
				int DAY1TOHOUR = settings.getInt("DAY1TOHOUR", 15);
				int DAY1TOMIN = settings.getInt("DAY1TOMIN", 8);

				int DAY2FROMHOUR = settings.getInt("DAY2FROMHOUR", 8);
				int DAY2FROMMIN = settings.getInt("DAY2FROMMIN", 54);
				int DAY2TOHOUR = settings.getInt("DAY2TOHOUR", 15);
				int DAY2TOMIN = settings.getInt("DAY2TOMIN", 8);

				int DAY1AFROMHOUR = settings.getInt("DAY1AFROMHOUR", 8);
				int DAY1AFROMMIN = settings.getInt("DAY1AFROMMIN", 54);
				int DAY1ATOHOUR = settings.getInt("DAY1ATOHOUR", 15);
				int DAY1ATOMIN = settings.getInt("DAY1ATOMIN", 8);

				int DAY2AFROMHOUR = settings.getInt("DAY2AFROMHOUR", 8);
				int DAY2AFROMMIN = settings.getInt("DAY2AFROMMIN", 54);
				int DAY2ATOHOUR = settings.getInt("DAY2ATOHOUR", 15);
				int DAY2ATOMIN = settings.getInt("DAY2ATOMIN", 8);

				if (intent.hasExtra("state")) {
					if (intent.getIntExtra("state", 0) == 0) {
						editor.putBoolean("ear", false);
					} else if (intent.getIntExtra("state", 0) == 1) {
						editor.putBoolean("ear", true);
					}
					editor.commit();
				}

				String action = intent.getAction();
				if (action.equals(Intent.ACTION_TIME_TICK)) {

					final Calendar c = Calendar.getInstance();
					Day = c.get(Calendar.DAY_OF_YEAR);
					Year = c.get(Calendar.YEAR);
					D_W = c.get(Calendar.DAY_OF_WEEK);
					Hour = c.get(Calendar.HOUR_OF_DAY);
					Minute = c.get(Calendar.MINUTE);
					Month = c.get(Calendar.MONTH);
					D_M = c.get(Calendar.DAY_OF_MONTH);

					Resources res = getResources();
					int[] a201304day1 = res.getIntArray(R.array.A201304Day1);
					int[] a201305day1 = res.getIntArray(R.array.A201305Day1);
					int[] a201306day1 = res.getIntArray(R.array.A201306Day1);
					int[] a201310day1 = res.getIntArray(R.array.A201310Day1);
					int[] a201304day2 = res.getIntArray(R.array.A201304Day2);
					int[] a201305day2 = res.getIntArray(R.array.A201305Day2);
					int[] a201306day2 = res.getIntArray(R.array.A201306Day2);
					int[] a201310day2 = res.getIntArray(R.array.A201310Day2);
					int[] a201304day1A = res.getIntArray(R.array.A201304Day1A);
					int[] a201305day1A = res.getIntArray(R.array.A201305Day1A);
					int[] a201306day1A = res.getIntArray(R.array.A201306Day1A);
					int[] a201310day1A = res.getIntArray(R.array.A201310Day1A);
					int[] a201304day2A = res.getIntArray(R.array.A201304Day2A);
					int[] a201305day2A = res.getIntArray(R.array.A201305Day2A);
					int[] a201306day2A = res.getIntArray(R.array.A201306Day2A);
					int[] a201310day2A = res.getIntArray(R.array.A201310Day2A);
					int[] IPP = res.getIntArray(R.array.IPP);
					int[] Pro_D = res.getIntArray(R.array.Pro_D);
					if (Year == 2013) {
						for (int o = 0; o < Pro_D.length; o++) {
							if (Day == Pro_D[o] | D_W == Calendar.SATURDAY
									| D_W == Calendar.SUNDAY) {
							} else if (o + 1 == Pro_D.length) {
								// If it is not Saturday, Sunday or Pro-D day.
								for (int a = 0; a < IPP.length; a++) {
									if (Day == IPP[a]) {
										if (Hour == IPPDAYFROMHOUR
												&& Minute == IPPDAYFROMMIN) {
											Start();
										}
										if (Hour >= IPPDAYTOHOUR
												&& Minute == IPPDAYTOMIN) {
											Over();
										}
									} else if (a + 1 == IPP.length) {
										// If it is not IPP day.
										if (Month == Calendar.APRIL) {
											for (int i = 0; i < a201304day1.length; i++) {
												if (D_M == a201304day1[i]) {
													if (Hour == DAY1FROMHOUR
															&& Minute == DAY1FROMMIN) {
														Start();
													}
													if (Hour >= DAY1TOHOUR
															&& Minute == DAY1TOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201304day2.length; i++) {
												if (D_M == a201304day2[i]) {
													if (Hour == DAY2FROMHOUR
															&& Minute == DAY2FROMMIN) {
														Start();
													}
													if (Hour >= DAY2TOHOUR
															&& Minute == DAY2TOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201304day1A.length; i++) {
												if (D_M == a201304day1A[i]) {
													if (Hour == DAY1AFROMHOUR
															&& Minute == DAY1AFROMMIN) {
														Start();
													}
													if (Hour >= DAY1ATOHOUR
															&& Minute == DAY1ATOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201304day2A.length; i++) {
												if (D_M == a201304day2A[i]) {
													if (Hour == DAY2AFROMHOUR
															&& Minute == DAY2AFROMMIN) {
														Start();
													}
													if (Hour >= DAY2ATOHOUR
															&& Minute == DAY2ATOMIN) {
														Over();
													}
												}
											}

										}

										if (Month == Calendar.MAY) {
											for (int i = 0; i < a201305day1.length; i++) {
												if (D_M == a201305day1[i]) {
													if (Hour == DAY1FROMHOUR
															&& Minute == DAY1FROMMIN) {
														Start();
													}
													if (Hour >= DAY1TOHOUR
															&& Minute == DAY1TOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201305day2.length; i++) {
												if (D_M == a201305day2[i]) {
													if (Hour == DAY2FROMHOUR
															&& Minute == DAY2FROMMIN) {
														Start();
													}
													if (Hour >= DAY2TOHOUR
															&& Minute == DAY2TOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201305day1A.length; i++) {
												if (D_M == a201305day1A[i]) {
													if (Hour == DAY1AFROMHOUR
															&& Minute == DAY1AFROMMIN) {
														Start();
													}
													if (Hour >= DAY1ATOHOUR
															&& Minute == DAY1ATOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201305day2A.length; i++) {
												if (D_M == a201305day2A[i]) {
													if (Hour == DAY2AFROMHOUR
															&& Minute == DAY2AFROMMIN) {
														Start();
													}
													if (Hour >= DAY2TOHOUR
															&& Minute == DAY2ATOMIN) {
														Over();
													}
												}
											}

										}

										if (Month == Calendar.JUNE) {
											for (int i = 0; i < a201306day1.length; i++) {
												if (D_M == a201306day1[i]) {
													if (Hour == DAY1FROMHOUR
															&& Minute == DAY1FROMMIN) {
														Start();
													}
													if (Hour >= DAY1TOHOUR
															&& Minute == DAY1TOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201306day2.length; i++) {
												if (D_M == a201306day2[i]) {
													if (Hour == 9) {
														Start();
													}
													if (Hour == 15) {
														if (Minute == 3) {
															Over();
														}
													}
												}
											}
											for (int i = 0; i < a201306day1A.length; i++) {
												if (D_M == a201306day1A[i]) {
													if (Hour == 9) {
														Start();
													}
													if (Hour == 15) {
														if (Minute == 3) {
															Over();
														}
													}
												}
											}
											for (int i = 0; i < a201306day2A.length; i++) {
												if (D_M == a201306day2A[i]) {
													if (Hour == 9) {
														Start();
													}
													if (Hour == 15) {
														if (Minute == 3) {
															Over();
														}
													}
												}
											}
										}
										if (Month == Calendar.OCTOBER) {
											for (int i = 0; i < a201310day1.length; i++) {
												if (D_M == a201310day1[i]) {
													if (Hour == DAY1FROMHOUR
															&& Minute == DAY1FROMMIN) {
														Start();
													}
													if (Hour >= DAY1TOHOUR
															&& Minute == DAY1TOMIN) {
														Over();
													}
												}
											}
											for (int i = 0; i < a201310day2.length; i++) {
												if (D_M == a201310day2[i]) {
													if (Hour == 9) {
														Start();
													}
													if (Hour == 15) {
														if (Minute == 3) {
															Over();
														}
													}
												}
											}
											for (int i = 0; i < a201310day1A.length; i++) {
												if (D_M == a201310day1A[i]) {
													if (Hour == 9) {
														Start();
													}
													if (Hour == 15) {
														if (Minute == 3) {
															Over();
														}
													}
												}
											}
											for (int i = 0; i < a201310day2A.length; i++) {
												if (D_M == a201310day2A[i]) {
													if (Hour == 9) {
														Start();
													}
													if (Hour == 15) {
														if (Minute == 3) {
															Over();
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		};
		IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
		IntentFilter intentFilter2 = new IntentFilter(Intent.ACTION_TIME_TICK);
		registerReceiver(receiver, intentFilter);
		registerReceiver(receiver, intentFilter2);
		super.onStart(intent, startId);
	}

	private void Start() {
		AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		Mode = audio.getRingerMode();
		editor.putBoolean("AUDIO", false);
		editor.putInt("MUSIC_V",
				audio.getStreamVolume(AudioManager.STREAM_MUSIC));
		editor.commit();
		if (Mode == 2) {
			if (ear == false) {
				for (int i = 0; i < 20; i++) {
					audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
							AudioManager.ADJUST_LOWER, 0);
				}
			}
			audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}

	private void Over() {
		AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		Mode = audio.getRingerMode();

		int MUSIC_V = settings.getInt("MUSIC_V",
				audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC));

		editor.putBoolean("AUDIO", true);
		editor.commit();

		if (Mode == 2) {
		} else {
			if (ear = false) {
				for (int i = 0; i < 20; i++) {
					audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
							MUSIC_V, 0);
				}
			}
			audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		}
	}

	@Override
	public void onDestroy() {
		this.unregisterReceiver(receiver);
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
